﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Data.SqlClient;

namespace Daneas.Utility.Universals
{
    /// <summary>
    /// 对象操作类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ObjectOper<T> where T : new()
    {
        /// <summary>
        /// 克隆list
        /// </summary>
        /// <param name="list">克隆对象</param>
        /// <returns></returns>
        public static List<T> ListClone(IList list)
        {
            if (list == null || list.Count <= 0)
            {
                return null;
            }

            List<T> b = new List<T>();
            Type type = typeof(T);

            foreach (var item in list)
            {
                T t = new T();
                PropertyInfo[] propertys = t.GetType().GetProperties();

                foreach (PropertyInfo p in propertys)
                {
                    object iValue = item;
                    p.SetValue(t, iValue, null);
                }

                b.Add(t);
            }
            return b;
        }

        /// <summary>
        /// 克隆Sqlparameter
        /// </summary>
        /// <param name="param">克隆对象</param>
        /// <returns></returns>
        public static SqlParameter[] SqlParametersClone(SqlParameter[] param)
        {
            if (param == null || param.Length <= 0)
            {
                return null;
            }

            List<SqlParameter> newP = new List<SqlParameter> { };

            foreach (SqlParameter p in param)
            {
                SqlParameter t = new SqlParameter();
                t.SqlDbType = p.SqlDbType;
                t.SqlValue = p.SqlValue;
                t.Value = p.Value;
                t.DbType = p.DbType;
                t.ParameterName = p.ParameterName;
                t.Size = p.Size;
                t.SourceColumn = p.SourceColumn;

                newP.Add(t);
            }

            return newP.ToArray();
        }

        /// <summary>
        /// Sqlparameter克隆
        /// </summary>
        /// <param name="param">克隆对象</param>
        /// <returns></returns>
        public static SqlParameter SqlparameterClone(SqlParameter param)
        {
            if (param == null)
            {
                return null;
            }

            SqlParameter[] ps = new SqlParameter[] { param };

            return SqlParametersClone(ps)[0];
        }
    }
}
